package cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.search.result;

import java.util.List;

import org.apache.commons.lang3.Validate;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import cz.muni.fi.pa165.musiclibrary.api.service.dto.ISongDTO;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.album.AlbumPage;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.artist.ArtistPage;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.song.SongPage;

/**
 * Panel presents basic information about a list of songs.
 * 
 * @author Maros Kucbel
 * @date 2012-11-22T22:13:55+0100
 */
public final class SongsPanel extends Panel {

    private List<ISongDTO> songs;
    
    /**
     * Constructor.
     * @param id ID of this panel.
     * @param songs List of songs to show.
     */
    public SongsPanel(final String id, List<ISongDTO> songs) {
        super(id);
        Validate.isTrue(songs != null, "List of songs should not be null.");
        Validate.noNullElements(songs, "List of songs should not contain null elements");
        this.songs = songs;
    }

    @Override
    protected void onInitialize() {
        super.onInitialize();
        this.initComponents();
    }
    
    private void initComponents() {
        ListView<ISongDTO> songsView = new ListView<ISongDTO>(ComponentIDs.SONGS_VIEW, this.songs) {

            @Override
            protected void populateItem(ListItem<ISongDTO> item) {
                ISongDTO song = item.getModelObject();
                
                PageParameters params = new PageParameters();
                params.add("id", song.getIdSong().toString());
                Link<Void> songLink = new BookmarkablePageLink<Void>(ComponentIDs.SONG_LINK, SongPage.class, params);
                songLink.add(new Label(ComponentIDs.SONG_LINK_NAME, song.getTitle()));
                item.add(songLink);
                
                params = new PageParameters();
                params.add("id", song.getArtist().getIdArtist().toString());
                Link<Void> artistLink = new BookmarkablePageLink<Void>(ComponentIDs.ARTIST_LINK, ArtistPage.class, params);
                artistLink.add(new Label(ComponentIDs.ARTIST_LINK_NAME, song.getArtist().getName()));
                item.add(artistLink);
                
                params = new PageParameters();
                params.add("id", song.getAlbum().getIdAlbum().toString());
                Link<Void> albumLink = new BookmarkablePageLink<Void>(ComponentIDs.ALBUM_LINK, AlbumPage.class, params);
                albumLink.add(new Label(ComponentIDs.ALBUM_LINK_NAME, song.getAlbum().getTitle()));
                item.add(albumLink);
            }
        };
        this.add(songsView);
    }
    
    /** Identifiers of added components. */
    private static final class ComponentIDs {
        /** View of songs. */
        private static final String SONGS_VIEW = "songsView";
        /** Link to song detail page. */
        private static final String SONG_LINK = "songLink";
        /** Name of the song. */
        private static final String SONG_LINK_NAME = "songLinkName";
        /** Link to artist detail page. */
        private static final String ARTIST_LINK = "artistLink";
        /** Name of the artist. */
        private static final String ARTIST_LINK_NAME = "artistLinkName";
        /** Link to album detail page. */
        private static final String ALBUM_LINK = "albumLink";
        /** Name of the album. */
        private static final String ALBUM_LINK_NAME = "albumLinkName";
    }
    
}